import type { IColumnsOption } from 'vc';
import { INDEX, COMPANY_NAME, INFO_SOURCE, PROVINCE, CITY, COUNTRY, INDUSTRY_CATEGORY, BUSINESS_DATA, REGISTERED_CAPITAL, toF9, toLawDetail } from '@/utils';

export function useTable() {
  const tableData = ref([]);

  const columns = ref<IColumnsOption[]>([
    { ...INDEX },
    { ...COMPANY_NAME },
    {
      title: '披露日期',
      field: 'noticeDate',
      width: 100,
      align: 'center',
      sortable: true,
      sortBy: 'noticeDate'
    },
    {
      title: '处罚决定日',
      field: 'penaltyDate',
      width: 100,
      align: 'center',
      sortable: true,
      sortBy: 'penaltyDate'
    },
    {
      title: '违法类型',
      field: 'reasonForViolation',
      width: 250,
      showOverflow: 'title'
    },
    {
      title: '处罚类型',
      field: 'penaltyResult',
      width: 150,
      slots: {
        default: ({ row }) => {
          let showResult = '-';
          if (row.penaltyResult && row.penaltyResult.length) {
            showResult = row.penaltyResult.map((o) => o.name).join(', ');
          }
          return showResult;
        }
      }
    },
    {
      title: '处罚金额',
      field: 'penaltyAmount',
      width: 100,
      align: 'center',
      sortable: true,
      sortBy: 'penaltyAmount',
      slots: {
        default: ({ row }) => {
          let showResult = '-';
          if (row.penaltyAmount) {
            showResult = row.penaltyAmount + '万';
          }
          return showResult;
        }
      }
    },
    {
      title: '违法依据',
      field: 'relevantLawsIllegalActivity',
      width: 250,
      slots: {
        default: ({ row }) => {
          if (row.relevantLawsIllegalActivities?.length === 0) {
            return '-';
          }
          return (
            row?.relevantLawsIllegalActivities?.map((item) => {
              if (item.lawUrl) {
                return <a title={item.lawName} class="companyName line-clamp" onClick={() => toLawDetail(encodeURIComponent(JSON.stringify(item)))} v-html={item.lawName || '-'}></a>;
              } else {
                return <span title={item.lawName || ''} v-html={item.lawName || '-'} class="line-clamp"></span>;
              }
            }) || <span title={row.relevantLawsIllegalActivity || ''} v-html={row.relevantLawsIllegalActivity || '-'} class="line-clamp"></span>
          );
        }
      }
    },
    {
      title: '处罚依据',
      field: 'relevantLaw',
      width: 250,
      showOverflow: 'title',
      slots: {
        default: ({ row }) => {
          if (row.relevantLaws?.length === 0) {
            return '-';
          }
          return (
            row?.relevantLaws?.map((item) => {
              if (item.lawUrl) {
                return <a title={item.lawName} class="companyName line-clamp" onClick={() => toLawDetail(encodeURIComponent(JSON.stringify(item)))} v-html={item.lawName || '-'}></a>;
              } else {
                return <span title={item.lawName || ''} v-html={item.lawName || '-'} class="line-clamp"></span>;
              }
            }) || <span title={row.relevantLaw || ''} v-html={row.relevantLaw || '-'} class="line-clamp"></span>
          );
        }
      }
    },
    {
      title: '违法事实',
      field: 'violationReasonSummary',
      width: 250,
      showOverflow: 'title'
    },
    {
      title: '处罚内容',
      field: 'approvalSummary',
      width: 250,
      showOverflow: 'title'
    },
    {
      title: '决定文书号',
      field: 'documentNumber',
      width: 150,
      showOverflow: 'title'
    },
    {
      title: '处罚机关',
      field: 'competentDepartment',
      width: 150,
      align: 'center',
      showOverflow: 'title'
    },
    { ...INFO_SOURCE('title') },
    { ...PROVINCE },
    { ...CITY },
    { ...COUNTRY },
    { ...INDUSTRY_CATEGORY },
    { ...BUSINESS_DATA },
    { ...REGISTERED_CAPITAL }
  ]);

  return {
    tableData,
    columns
  };
}
